package tpi.gui.carmensd.ui

import android.app.Activity
import android.os.Bundle
import org.eclipse.xtend.lib.annotations.Accessors
import retrofit.RestAdapter
import tpi.gui.carmensd.R
import tpi.gui.carmensd.dto.Pais
import tpi.gui.carmensd.service.CarmenSandiegoService
import android.widget.ArrayAdapter
import android.widget.Spinner
import retrofit.Callback
import retrofit.RetrofitError
import retrofit.client.Response
import android.view.View
import android.content.Intent
import tpi.gui.carmensd.utils.Utils
import android.widget.Button
import tpi.gui.carmensd.dto.OrdenDeArresto
import android.widget.TextView
import tpi.gui.carmensd.dto.Villano

@Accessors
class AeropuertoActivity extends Activity {
	
	Pais paisActual
	Villano acusado
	String rutaRecorrida
	CarmenSandiegoService service
	extension Utils = new Utils
	
	override void onCreate(Bundle savedInstanceState) {
    	super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_aeropuerto)
                
        service = createService()
        pedidoPaisActual[| cargarDestinos() ]
        ordenDeArresto[| acusadoNombre(); estadoBotonInterpol() ]
        pedidoRuta[| cargarRutaRecorrida() ]
        
    }
    
    def pedidoPaisActual(()=>void action) {
    	service.getPaisActual(new Callback<Pais>() {	
						override failure(RetrofitError e) {
							e.printStackTrace
						}
						override success(Pais pais, Response response) {
							setPaisActual(pais)
							action.apply()
						}
    	})
    }
    
    def viajar(View view) {
    	val spinner = findViewById(R.id.destinos) as Spinner
        service.viajar(spinner.selectedItem as String, 
        				new Callback<Pais>(){			
									override failure(RetrofitError e) {
										e.printStackTrace
									}
									override success(Pais Pais, Response response) {
										efectuarViaje()
									}
						}
		)
    } 
    
    def pedidoRuta(()=>void action) {
    	service.getRutaRecorrida(new Callback<String>() {	
						override failure(RetrofitError e) {
							e.printStackTrace
						}
						override success(String ruta, Response response) {
							setRutaRecorrida(ruta)
							action.apply()
						}
    	})
    }
    
    def cargarRutaRecorrida() {
    	findViewById(R.id.ruta) as TextView => [
    		text = rutaRecorrida
    	]
    }
    
    def irAlPaisActual(View view) {
    	new Intent(this, PistasActivity).startActivity()
    }
    
    def efectuarViaje() {
		new Intent(this, PistasActivity).startActivity()
    }
    
    def cargarDestinos() {
		findViewById(R.id.destinos) as Spinner => [
			adapter = new ArrayAdapter<String>(this,
	                               android.R.layout.simple_spinner_item,
	                               paisActual.nombresConexiones)
        ]
        
        findViewById(R.id.paisActual) as Button => [
        	text = paisActual.nombre
        ]
	}
	
	def ordenDeArresto(()=>void action) {
		service.getAcusado(
	       	new Callback<Villano>() {
	       		override failure(RetrofitError e) {
	       			e.printStackTrace
	       		}
	       		override success(Villano villano, Response response) {
	       			setAcusado(villano)
	       			action.apply()
	       		}
	       	}
		)
	}

	def acusadoNombre() {
		findViewById(R.id.acusado) as TextView => [
				text = acusado.nombre
		]
	}
	
	def estadoBotonInterpol() {
		findViewById(R.id.interpol) as Button => [
			enabled = acusado.nombre == "Desconocido"
		]
	}

	def irALaInterpol(View view) {
		new Intent(this, InterpolActivity).startActivity()
	}
	
}